from time import sleep
from page.base_page import BasePage
from selenium.webdriver.common.by import By

class BusinessPage(BasePage):
    """商机页面"""
    url = '/crm5/index.php?m=business'

    """定位器"""
    # 添加商机
    add_business_locator = (By.XPATH, '/html/body/div[5]/div[2]/div[1]/div/a')
    # 查看
    look_locator = (By.XPATH, '//*[@id="form1"]/table/tbody/tr[1]/td[12]/a[1]')
    # 推进
    advance_locator = (By.XPATH, '//*[@id="form1"]/table/tbody/tr[1]/td[12]/a[2]')
    # 推进按钮
    btn_save_button_locator = (By.NAME, 'submit')
    # 今日需联系
    contact_today_locator = (By.CSS_SELECTOR, 'body > div.container > p > a:nth-child(9)')
    # 商机工具
    business_tools_locator = (By.CSS_SELECTOR, 'body > div.container > div.row > div:nth-child(1) > div > div > button')
    # 导出
    exported_locator = (By.ID, 'excelExport')
    # 批量选择
    check_all_locator = (By.ID, 'check_all')
    # 删除
    delete_locator = (By.XPATH, '//*[@id="delete"]')
    #添加商机成功文本
    success_tip_locator = (By.CSS_SELECTOR,'body > div.container > div.alert.alert-success')
    #删除数据
    delete_tip_locator =(By.XPATH,'//*[@id="form1"]/table/tbody/tr/td')
    #推进成功文本
    advance_success_tip_locator = (By.XPATH,'/html/body/div[5]/div[2]')

    def click_add_business(self):
        """添加商机"""
        self.driver.find_element(*self.add_business_locator).click()

    def click_look(self):
        """点击查看"""
        self.driver.find_element(*self.look_locator).click()

    def get_url_look(self):
        """获取查看页面的url"""
        self.driver.find_element(*self.look_locator).click()
        sleep(2)
        return self.driver.current_url

    def click_advance(self):
        """点击推进"""
        self.driver.find_element(*self.advance_locator).click()

    def click_btn_advance(self):
        """点击推进按钮"""
        self.driver.find_element(*self.btn_save_button_locator).click()

    def click_contact_today(self):
        """点击今日需联系"""
        self.driver.find_element(*self.contact_today_locator).click()

    def click_business_tools(self):
        """点击商机工具"""
        self.driver.find_element(*self.business_tools_locator).click()

    def click_exported(self):
        """点击导出"""
        self.driver.find_element(*self.exported_locator).click()
        self.driver.switch_to.alert.accept()  # 确认弹窗

    def click_check_all(self):
        """点击批量选择"""
        self.driver.find_element(*self.check_all_locator).click()

    def click_delete(self):
        """点击删除"""
        self.driver.find_element(*self.delete_locator).click()
        self.driver.switch_to.alert.accept()   #确认弹窗

    def get_success_tip(self):
        """获取 添加商机成功文本"""
        return self.driver.find_element(*self.success_tip_locator).text

    def click_delete_tip(self):
        #获取删除数据后的文本
        return self.driver.find_element(*self.delete_tip_locator).text

    def get_advance_success_tip(self):
        """获取推进成功文本"""
        return self.driver.find_element(*self.advance_success_tip_locator).text